﻿var cookieJson = {
    "portaluserno": "admin",
    "portalusername": "系统管理员",
    "portalusermanager": "2",
    "portaluserdept" : "101010"
}
// document.documentElement.onclick = function () {
//     if (getCookie('portaluserno') == '') {
//         top.location.href = location.href;
//         location.href = '${request.contextPath}/logout?service=http://192.168.100.111:8889/sw/index'; 
//         http://192.168.100.111:8000/swsso/login?service=http%3A%2F%2F192.168.100.111%3A8893%2F%24%257Brequest.contextPath%257D%2Flogout%3Fservice%3Dhttp%3A%2F%2F192.168.100.111%3A8889%2Fsw%2Findex
//         http://192.168.100.111:8000/swsso/login?service=http%3A%2F%2F192.168.100.111%3A8000%2Fswsso%2Fadmin%2Fadmin
//         sessionStorage.setItem("setCookieType", "false");
//     }   
// }


function setCookie(cname, cvalue, hours) {
    var d = new Date();
    d.setTime(d.getTime() + (hours * 60 * 60 * 1000));
    var expires = "expires=" + d.toGMTString();
    document.cookie = cname + "=" + cvalue + "; " + expires;
}

function getCookie(cname) {
    return cookieJson[cname];
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i].trim();
        if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
    }
    return "";
}

document.oncontextmenu = function() {
    return false;
}

function deleteCookie(cookieName) {
    document.cookie = cookieName + "=; expires=" + new Date(0).toGMTString();
}
if (typeof ($) != 'undefined') {
    $.ajaxSettings.crossDomain = true;
    $.ajaxSettings.xhrFields = {
        withCredentials: true
    };
    $(function () {
        if (window.location.href.toLowerCase().indexOf("login.html") > -1) {
            // deleteCookie("portaluserno");
            // deleteCookie("portalusername");
            // deleteCookie("portalusermanager");
            // deleteCookie("portaluserdept");
        }
        // 立即登录
        $('#loginDiv .loginBtn').on('click', function () {
            login();
        });
        $('.loginInput').bind('keydown', function (e) {
            var key = e.which;
            if (key == 13) {
                e.preventDefault();
                login();
            }
        });
        $("#rememberuserName").on('click', function () {
            var chknamestatus = $(this).is(":checked");
        });
        $("#rememberPassword").on('click', function () {
            var chkpasswordstatus = $(this).is(":checked");
        });

        $('#menuHandle').click(function () {
            if ($("#menuHandle").hasClass("layui-icon-shrink-right")) {
                $("#menuHandle").removeClass("layui-icon-shrink-right");
                $("#menuHandle").addClass("layui-icon-spread-left");
                $("#menuHandle").attr('title', '展开菜单栏');
                $(".treeNodeA").animate({ opacity: 0 });
                $("#menuTip").animate({ width: 0 });
                $("#menuUL").animate({ width: 0 });
                $("#tabTip").animate({ width: $(window).width() - 8 });
            } else {
                $("#menuHandle").removeClass("layui-icon-spread-left");
                $("#menuHandle").addClass("layui-icon-shrink-right");
                $("#menuHandle").attr('title', '折叠菜单栏');
                $(".treeNodeA").animate({ opacity: 1 });
                $("#menuUL").animate({ width: '200px' });
                $("#menuTip").animate({ width: '200px' });
                //$("#tabTip").animate({ width: 'calc(100% - 210px)' });
                $("#tabTip").animate({ width: $(window).width() - 210 });
            }

        });
    });
}

//读取Json文件
function InitialParameter(url) {
    var info = null;

    $.ajax({
        async: false,
        url: url,
        success: function (result) {
            info = result;
        },
        error: function (result) {
            console.log(result);
        }
    });
    return info;
}

function login(){
    var username = $('#username').val();
    var password = $('#password').val();
    if (!username) {
        $('#username').focus();
        layer.msg('请输入用户名', {
            shade: 0.3,
            time: 1500
        });
        return;
    }
    if (!password) {
        $('#password').focus();
        layer.msg('请输入密码', {
            shade: 0.3,
            time: 1500
        });
        return;
    }
    var configInfo = InitialParameter("/Config.json");
    let modelApi = configInfo["modelTree"].type + '://' + configInfo["modelTree"].value;
    var globleUrl = modelApi + "/sw/";
    $.ajax({
        type: "post",
        async: false,
        xhrFields: {
            withCredentials: true
        },
        crossDomain: true,
        url: globleUrl + "/login/login",
        data: {
            no: username,
            passWord: password
        },
        header: {
            "Access-Control-Allow-Credentials" : true,
            "Content-Type": "application/x-www-form-urlencoded"
        },
        success: function (result) {
            result = JSON.parse(result);
            if (result.code == 200) {
                setCookie("portaluserno", result.data.no, 2);
                setCookie("portalusername", result.data.name, 2);
                setCookie("portalusermanager", result.data.ismanager, 2);
                setCookie("portaluserdept", result.data.fkDept, 2);
                top.location.href = '/Main';
            } else {
                layer.msg(result.msg, {
                    shade: 0.3,
                    time: 2000
                });
            }
        },
        error: function (err) {
        }
    });
}

function getBaseplatformPort() {
    var configInfo = InitialParameter("/Config.json");
    let modelApi = configInfo["modelTree"].type + '://' + configInfo["modelTree"].value;
    var globleUrl = modelApi + "/sw/";
    return globleUrl;
}

function getModelOriginPort() {
    var configInfo = InitialParameter("/Config.json");
    let modelApi = configInfo["modelOrigin"].type + '://' + configInfo["modelOrigin"].value;
    var globleUrl = modelApi + "/sw/"; 
    return globleUrl;
}

function getFileUploadOriginPort() {
    var configInfo = InitialParameter("/Config.json");
    let modelApi = configInfo["fileUploadOrigin"].type + '://' + configInfo["fileUploadOrigin"].value;
    var globleUrl = modelApi + "/sw/";
    return globleUrl;
}

function getWSOriginPort() {
    var configInfo = InitialParameter("/Config.json");
    let modelApi = configInfo["wsOrigin"].type + '://' + configInfo["wsOrigin"].value;
    var globleUrl = modelApi + "/sw/";
    return globleUrl;
}

function getSpatialOriginPort() {
    var configInfo = InitialParameter("/Config.json");
    let modelApi = configInfo["spatialOrigin"].type + '://' + configInfo["spatialOrigin"].value;
    var globleUrl = modelApi + "/sw/";
    return globleUrl;
}

function getWordUploadOriginPort() {
    var configInfo = InitialParameter("/Config.json");
    let modelApi = configInfo["wordUploadOrigin"].type + '://' + configInfo["wordUploadOrigin"].value;
    var globleUrl = modelApi + "/sw/";
    return globleUrl;
}